#include <Riostream.h>
#include <TString.h>
#include <TSystem.h>
#include <TStopwatch.h>

#include "CpLog.h"
#include "CpFitter.h"

#include "CpFitMain.h"

int main(int argc, const char *argv[])
{

   TStopwatch timer;
   timer.Start();

   // init debug level
   Int_t debugLevel = 0;

   // default config file
   TString cfgFileName(gSystem->ExpandPathName("CpConfig.cfg"));

   // output directory (default is current directory)
   TString outputDir;

   // define Magentic Field map
   CpMagneticFieldMap fieldMap("MyTestField", "My Test Filed");

   // define fitter
   CpFitter fitter;

   // arguments handler
   TString tmp;

   for (Int_t i = 0; i < argc; i++) {
      tmp = argv[i];

      if (!tmp.CompareTo("--debug")) {
         tmp = argv[i + 1];
         debugLevel = tmp.Atoi();
         CpLog::SetClassDebugLevel("CpPolynom1D", CpLog::kDebug + debugLevel);
         CpLog::SetClassDebugLevel("CpPolynom2D", CpLog::kDebug + debugLevel);
         CpLog::SetClassDebugLevel("CpPolynom3D", CpLog::kDebug + debugLevel);
         CpLog::SetClassDebugLevel("CpFitter", CpLog::kDebug + debugLevel);
         CpLog::SetClassDebugLevel("CpMagneticFieldMap", CpLog::kDebug + debugLevel);
         CpLog::SetClassDebugLevel("CpMagneticFieldMapObject", CpLog::kDebug + debugLevel);
      } else if (!tmp.CompareTo("--config")) {
         cfgFileName = argv[i + 1];
      }
   }

   // opening config file
   ifstream fileIn;
   fileIn.open(cfgFileName.Data());

   if (!fileIn.is_open()) {
      Printf("Error : Config file '%s' was not found !!! Exiting ...", cfgFileName.Data());
      return 1;
   }

   TString fitStrings[16];
   Int_t count = 0;

   // Reading config file
   Char_t fileItem[256];
   TString fileItemStr, opt, value, rootname;
   TObjArray *array;

   while (fileIn.good()) {
      fileIn.getline(fileItem, 256);
      fileItemStr = fileItem;
      fileItemStr.ReplaceAll(" ", "");

      if (fileItemStr.IsNull()) continue;

      if (fileItemStr.BeginsWith("#")) continue;

      array = fileItemStr.Tokenize("=");
      opt = ((TObjString *) array->At(0))->GetString();
      value = ((TObjString *) array->At(1))->GetString();

      if (!opt.CompareTo("InputData")) {
         value.ReplaceAll("\"", "");
         TString rootname = value;

         if (value.EndsWith(".txt")) {
            rootname.ReplaceAll(".txt", ".root");
            fieldMap.ImportFromTxtFile(value.Data(), rootname.Data());
         }

         fieldMap.SetMagneticMapRootFileName(rootname.Data());
      } else if (!opt.CompareTo("PositionAxis")) {
         fitter.SetPositionsAxisFromString(value);
      } else if (!opt.CompareTo("MangetAxisIndex")) {
         fitter.SetfMagneticAxisIndex(value.Atoi());
      } else if (!opt.CompareTo("PolynomNumber")) {
         fitter.SetPolynomGradeNumber(value.Atoi());
      } else if (!opt.CompareTo("OutPutDir")) {
         value.ReplaceAll("\"", "");
         fitter.SetOutputDirName(value);
      } else if (!opt.CompareTo("OutPutFile")) {
         value.ReplaceAll("\"", "");
         fitter.SetOutputFileName(value);
      } else {
         Printf("Warning : %s not supported ", opt.Data());
      }

      fitStrings[count++] = fileItem;
   }

   fileIn.close();

   fitter.Run(&fieldMap);

   timer.Stop();
   timer.Print();
   return 0;

}
